<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2024/7/22
 * Time: 18:34
 */

namespace app\webactive\controller;

use app\api\controller\v2\Auth;
use app\common\cachemodel\AppCompensationGiveRecord;
use app\common\lib\exception\ApiException;
use think\Controller;
use think\Env;
use think\Log;

class CompensateActive extends Controller
{

    protected $test = false;

    public function index()
    {
        $headers = request()->header();
        $oss = Env::get('host.photo');
        $host = Env::get('host.domain');
        if ($this->test === false)
        {
            try
            {
                $auth = new Auth();
                $auth->checkRequestAuth();
                $uid = $auth->user['id'];
            }
            catch (\Exception $e)
            {
                Log::write('补偿活动授权认证异常：' . $e->getMessage() . '失败行数：' . $e->getLine(), 'error');
                throw new ApiException('服务异常');
            }
        } else
        {
            $uid = 1;
        }

        $endTime = strtotime('2024-07-30 00:00:00');

        $nowTime = $this->request->time();
        if ($nowTime > $endTime)
        {
            $status = 3;
        } else
        {
            $appCompensationGiveRecordModel = new AppCompensationGiveRecord();
            $status = $appCompensationGiveRecordModel->isGived($uid) ? 2 : 1;//1=>未领取 2=>已领取 3=>已过期
        }
        unset($headers['cookie'], $headers['time'], $headers['content-type'], $headers['content-length']);
        $headersJson = json_encode($headers);
        return $this->fetch('index', ['oss' => $oss, 'status' => $status, 'uid' => $uid, 'app_type' => $headers['app-type'] ?? '', 'headers' => $headersJson, 'host' => $host]);
    }
}
